#include "Module/DefaultModule.hpp"
#include "Module/ModuleRegistry.hpp"
#include "Module/ManageConnectionsCommand.qth"
#include "Module/SqlSvrConnConfigWidget.qth"
#include "AbsUI/UICommandRegistry.hpp"
#include "AbsUI/MainWindow.qth"
#include "AbsUI/ConnCfgWidgetFactory.hpp"
#include "Storage/ConnectionConfigFactory.hpp"
#include "Storage/ConnectionConfigCollection.hpp"
#include "Storage/SqlSvrConnectionConfig.hpp"
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>

namespace Module
{
	
const std::string DefaultModule::FILE_MENU_ID( "Menu.File");
const std::string DefaultModule::CONFIG_MGMT_CMD_ID( "Menu.File.ManageConnections");

const std::string& Module::DefaultModule::uid() const
{
	static std::string UID( "MiniCase.Default");
	return UID;
}

// ---------------------------------------------------------------------------------------------------------------------

void DefaultModule::registerUICommands()
{
	// TODO: String resource
	QMenu* pFileMenu = new QMenu( "File");
	ModuleRegistry::instance().uiCmdRegistry().registerMenu( FILE_MENU_ID, pFileMenu);
	ModuleRegistry::instance().mainWindow().menuBar()->addMenu( pFileMenu);
	
	AbsUI::UICommand* pConfigMgmtCmd = new Module::ManageConnectionsCommand;
	pFileMenu->addAction( pConfigMgmtCmd->boundAction());
	ModuleRegistry::instance().uiCmdRegistry().registerCommand( CONFIG_MGMT_CMD_ID, pConfigMgmtCmd);
}

// ---------------------------------------------------------------------------------------------------------------------

void DefaultModule::registerStorageCfgTypes()
{
	using Storage::SqlSvrConnectionConfig;

	ModuleRegistry::instance().connConfigFactory().registerConnCfgConstructor( 
		SqlSvrConnectionConfig::TYPENAME, &Storage::createConnectionConfig<SqlSvrConnectionConfig>);

	ModuleRegistry::instance().connCfgWidgetFactory().registerConstructor( 
		SqlSvrConnectionConfig::TYPENAME, &AbsUI::connCfgWidgetConstructor<SqlSvrConnConfigWidget>);
}

// ---------------------------------------------------------------------------------------------------------------------

void DefaultModule::initialize()
{
	AbstractModule::initialize();
	ModuleRegistry::instance().connCfgCollection().load( ModuleRegistry::instance().CONNECTIONS_FILE_PATH);
}


} // namespace Module
